﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using EasyTemplates;
using System.Reflection;

namespace TemplateTest
{
    [TestFixture]
    public class ParameterChange_Test
    {
        [Test]
        public void ChangeTemplate()
        {
            Template t = new Template();
            t.TemplateBody = "X";
            Assert.AreEqual("X", t.Generate());
            t.TemplateBody = "Y";
            Assert.AreEqual("Y", t.Generate());
        }

        [Test]
        public void AddParameter()
        {
            Template t = new Template();
            t.TemplateBody = "X";
            Assert.AreEqual("X", t.Generate());
            t.Parameters.Add("Y", new Parameter(5));
            Assert.AreEqual("X", t.Generate());
        }

        [Test]
        public void RenameParameter()
        {
            Template t = new Template();
            t.TemplateBody = "X";
            t.Parameters.Add("Y", new Parameter(5));
            Assert.AreEqual("X", t.Generate());
            t.Parameters.Remove("Y");
            t.Parameters.Add( "Z", new Parameter(5) );
            Assert.AreEqual("X", t.Generate());
        }

        public class BaseClass
        {
            public string P1;
        }

        public class Derived : BaseClass
        {
            public string P2;
        }

        [Test]
        public void ChangeToDerivedType()
        {
            Template t = new Template(
                "<%=P.P1%>",
                "P", new BaseClass() { P1 = "X" }
            );
            Assert.AreEqual("X", t.Generate());

            t.Parameters["P"] = new Parameter(
                new Derived() { P1 = "Y" }
            );
            Assert.AreEqual( "Y", t.Generate() );
        }

        [Test]
        public void ChangeToSameType()
        {
            Template t = new Template();
            t.TemplateBody = "<%=Y%>";
            t.Parameters.Add("Y", new Parameter(5));
            Assert.AreEqual("5", t.Generate());
            t.Parameters["Y"] = new Parameter(7);
            Assert.AreEqual("7", t.Generate());
        }

        [Test]
        public void ChangeToBaseType()
        {
            Template t = new Template(
                "<%=P.P1%>",
                "P", new Derived() { P1 = "X" }
            );
            Assert.AreEqual("X", t.Generate());

            t.Parameters["P"] = new Parameter(
                new BaseClass() { P1 = "Y" }
            );
            Assert.AreEqual("Y", t.Generate());
        }
    }
}
